/*
 ----------------------------------------------------------------------------------
                          "THE BEER-WARE COPYRIGHT LICENSE"
                                    (Revision 43)
                                     -----------
   Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
   this file has been sent directly to You by the author, You can do whatever You
   want with this stuff as long as You don't sell or rent it. If we meet some day,
   and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                __________
                                                                Paweł Rymaszewski
 ----------------------------------------------------------------------------------
 */

package ui.windows.forms.searchForm;

import exceptions.NoMessageException;
import processing.objects.Message;
import processing.objects.SearchParameters;
import ui.messages.MessageBoxes;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

/**
 * Action listener class for SearchForm class
 *
 * @author rymek
 *         User: Rymek
 *         Date: 23.07.11
 *         Time: 17:05
 */
class SearchFormActionListener implements ActionListener {

    private final SearchForm searchForm;        // parent Search Form

    /**
     * Constructor.
     * Sets reference to parent form
     *
     * @param searchForm parent form
     */
    SearchFormActionListener(SearchForm searchForm) {
        this.searchForm = searchForm;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.searchForm.getSearchButton()) this.searchAction();
        if (e.getSource() == this.searchForm.getStopButton()) this.stopAction();

    }

    /**
     * Performs search action
     */
    void searchAction() {
        try {

            Message message = this.searchForm.getMessageInformationPanel().buildMessageInfo();
            SearchParameters searchParameters = this.searchForm.getSearchParamsPanel().getSearchParameters();
            searchParameters.setMessageInfo(message);

            this.searchForm.performSearch(searchParameters);

        } catch (NoMessageException error) {
            MessageBoxes.noMessageException(error);

        } catch (Exception error) {
            //MessageBoxes.genericErrorMessage(error);
        }
    }

    /**
     * Performs Stop Search action
     */
    private void stopAction() {
        try {

            this.searchForm.terminateSearch();

        } catch (SQLException ignored) {
        }
    }
}
